<script setup>
import { onMounted, onUnmounted, reactive, ref } from 'vue'

// ======== 业务1：显示鼠标坐标 ========
const mouse = reactive({
  x: 0,
  y: 0,
})
const handleMouse = e => {
  mouse.x = e.clientX
  mouse.y = e.clientY
}
// DOM挂载完毕后，再添加 DOM 事件
onMounted(() => {
  document.addEventListener('mousemove', handleMouse)
})
onUnmounted(() => {
  document.removeEventListener('mousemove', handleMouse)
})

// ======== 业务2：计数器 ========
const count = ref(0)
const btn = () => {
  count.value++
}
</script>

<template>
  <div>当前鼠标位置</div>
  <div>x: {{ mouse.x }}</div>
  <div>y: {{ mouse.y }}</div>
  <div>当前点击次数：{{ count }}</div>
  <button @click="btn">点击</button>
</template>
